package com.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.api.param.RankParam;
import com.api.viewModel.LoginVM;
import com.api.viewModel.StoreVM;
import com.example.apple.ddhr_new.R;
import com.example.apple.ddhr_new.utils.ToastUtil;
import com.example.lzbase.BaseActivity;
import com.example.lzbase.BaseViewModelActivity;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import co.lujun.androidtagview.TagContainerLayout;
import co.lujun.androidtagview.TagView;

public class TagActivity extends BaseViewModelActivity<StoreVM> {


    @BindView(R.id.ed_text)
    EditText edText;
    @BindView(R.id.tagcontainerLayout1)
    TagContainerLayout tagcontainerLayout1;

    @BindView(R.id.et_add)
    TextView etAdd;
    @BindView(R.id.tagcontainerLayout2)
    TagContainerLayout tagcontainerLayout2;


    private ArrayList<String> tags = new ArrayList<>();
    private ArrayList<String> selectString = new ArrayList<>();

    @Override
    protected int titleText() {
        return R.string.tianjia_biaoqian;
    }

    @Override
    protected void created(@Nullable Bundle savedInstanceState) {
        super.created(savedInstanceState);
        String param = getIntent().getExtras().getString("param");
        if (param != null && param.length() > 0) {
            selectString.addAll(Arrays.asList(StringUtils.split(param, ",")));

        }
//        String tag = CacheUtil.getTag(this);
//        if(tag!=null){
//            tags.addAll(Arrays.asList(StringUtils.split(tag, ",")));
//        }


        setTitleRight(getString(R.string.done));

        tagcontainerLayout1.setTags(selectString);


//        List<int[]> colors = new ArrayList<int[]>();
//        //int[]color = {backgroundColor, tagBorderColor, tagTextColor, tagSelectedBackgroundColor}
//
//        tags.forEach(res->{
//            boolean select=false;
//            int[] col1 = {getColor(R.color.white), getColor(R.color.color_172), getColor(R.color.color_cell_title), getColor(R.color.white)};
//            int[] col2 = {getColor(R.color.blue), getColor(R.color.color_172), getColor(R.color.color_cell_title), getColor(R.color.white)};
//
//
//            for(int i=0; i<selectString.size();i++){
//                if(res.equals(selectString.get(i))){
//                    select=true;
//                    break;
//                }
//            }
//            if(select){
//                colors.add(col2);
//            }else {
//                colors.add(col1);
//            }
//
//        });
        tagcontainerLayout1.setOnTagClickListener(new TagView.OnTagClickListener() {
            @Override
            public void onTagClick(int position, String text) {

            }

            @Override
            public void onTagLongClick(final int position, String text) {

            }

            @Override
            public void onSelectedTagDrag(int position, String text) {
            }

            @Override
            public void onTagCrossClick(int position) {

                selectString.remove(position);
                tagcontainerLayout1.removeTag(position);

            }
        });
        tagcontainerLayout2.setOnTagClickListener(new TagView.OnTagClickListener() {
            @Override
            public void onTagClick(int position, String text) {
                if (selectString.contains(text)) {
                    ToastUtil.toast(TagActivity.this, R.string.biao_qian_cun_zai);

                } else {
                    tagcontainerLayout1.addTag(text);
                    selectString.add(text);


                }
            }

            @Override
            public void onTagLongClick(int position, String text) {

            }

            @Override
            public void onSelectedTagDrag(int position, String text) {

            }

            @Override
            public void onTagCrossClick(int position) {

            }
        });

        viewModel.tags.observe(this,res->{

          ArrayList<String> datas=  new ArrayList<>();
          for (int i=0;i<res.size();i++)
          {
              datas.add(res.get(i).getTagname());
          }
            tagcontainerLayout2.setTags(datas);

        });
        RankParam param1 = new RankParam();
        param1.rank = App.getInstance().getAccount().getUserrank();

        addSubscription(viewModel.getTag(param1));


    }

    @Override
    protected void titleRightClick() {
        if (selectString.size() == 0) {
            ToastUtil.toast(this, R.string.qing_tian_jia_biao_qian);
            return;
        } else {
            Intent intent = new Intent();
            intent.putExtra("data", StringUtils.join(selectString, ","));
            setResult(1000, intent);
            finish();
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_tag;
    }


    @OnClick(R.id.et_add)
    public void onClick() {

        if (edText.getText().toString().trim().length() == 0) {
            return;
        } else {

            if (selectString.contains(edText.getText().toString().trim())) {
                ToastUtil.toast(this, R.string.biao_qian_cun_zai);
                return;
            } else {
                tagcontainerLayout1.addTag(edText.getText().toString().trim());
                selectString.add(edText.getText().toString().trim());
                edText.setText("");

            }


        }
    }



}
